﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WF.Common;

namespace WF.Web
{
    public partial class MainFormForIE6 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.MessageBar.Visible = false;   
        }

        public void ShowErrorMessage(string msg)
        {
            ShowErrorMessage(msg, false);
        }

        public void ShowInfoMessage(string msg)
        {
            ShowInfoMessage(msg, false);
        }

        public void ShowErrorMessage(string msg, bool popup)
        {
            if (!popup)
            {
                this.MessageBar.Visible = true;
                this.MessageBar.InnerText = "出错信息： " + msg;
            }
            else
            {
                this.MessageBar.Visible = false;
                string s = "alert('出错信息： {0}');";
                msg = HttpUtility.HtmlEncode(msg).Replace("'", "\\'");
                s = string.Format(s, msg);
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", s, true);
            }
        }

        public void ShowInfoMessage(string msg, bool popup)
        {
            if (!popup)
            {
                this.MessageBar.Visible = true;
                this.MessageBar.InnerText = "提示信息： " + msg;
            }
            else
            {
                this.MessageBar.Visible = false;
                string s = "alert('提示信息： {0}');";
                msg = HttpUtility.HtmlEncode(msg).Replace("'", "\\'");
                s = string.Format(s, msg);
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", s, true);
            }
        }

        /// <summary>
        /// 异常处理函数
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="popup">是否弹出窗口</param>
        public void HandleException(Exception ex, bool popup)
        {
            if (null != ex)
            {

                //如果异常类型是业务异常或者业务异常派生出来的异常，则不记日志，


                //如果是其他异常记录日志


                if ((null != ex.InnerException) && (!(ex.InnerException is BusinessException)))
                {
                    //输出日志
                    TraceHelper.TraceError(ex.ToString());
                }

                //取出所有的内部异常信息
                StringBuilder builder = new StringBuilder();
                //builder.AppendFormat("[{0}] ", ex.Message);
                for (Exception exception = ex; exception != null; exception = exception.InnerException)
                {
                    if (exception is BusinessException)
                    {
                        builder.Remove(0, builder.Length);
                        builder.AppendFormat("[{0}] ", exception.Message);
                        break;
                    }
                    else
                    {
                        builder.AppendFormat("[{0}] ", exception.Message);
                    }
                }

                //显示
                ShowErrorMessage(builder.ToString(), popup);
            }
        }

        /// <summary>
        /// ObjectDataSource异常处理函数
        /// </summary>
        /// <param name="e"></param>
        /// <param name="popup">是否弹出窗口</param>
        public void HandleException(ObjectDataSourceStatusEventArgs e, bool popup)
        {
            if (null != e.Exception)
            {
                HandleException(e.Exception, popup);
                e.ExceptionHandled = true;
            }
            //else
            //{
            //    ShowInfoMessage("操作成功", false);
            //}
        }
    }
}